<template>
	<view class="home-page">
		<uv-sticky
			customNavHeight="0"
		>
			<view>
				<!-- #ifdef APP -->
				<hsb-global-status-bar />
				<!-- #endif -->
				<hsb-business-home-header
					@city="toCitys"
					@search="toSearch"
				/>
			</view>
		</uv-sticky>
		<hsb-common-base-swiper
			:list="bannerList"
		/>
		<hsb-business-alibaba-notice />
		<hsb-business-recycle-category
			@recycle="toRecycle"
		/>
		<hsb-business-self-card
			:info="modelInfo"
			@recycle="toRecycle"
		/>
		<hsb-business-store-card
			v-if="hasStore"
			:info="storeData"
			@recycle="toRecycle"
		/>
		<hsb-business-recycle-progress
			@service="$store.dispatch('contactService')"
		/>
		<hsb-business-hsb-partner />
	</view>
</template>

<script>
import { mapState } from 'vuex'

export default {
	computed: {
		...mapState({
			userLocation: state => state.userLocation,
			userSelectLocation: state => state.userSelectLocation
		}),
		bannerList () {
			return this.bannerInfo['hsb_applet_01'] || []
		},
		hasStore () {
			return Boolean(this.storeData.storeInfo?.storeId || '')
		}
	},
	data () {
		return {
			bannerInfo: {},
			modelInfo: {},
			storeData: {}
		}
	},
	methods: {
		async getBannerList () {
			const [res] = await this.$apis.getBannerBatch({
				pid: '1196',
				platform: '2',
				positionCode: 'hsb_applet_01,hsb_wx_v2_category,hsb_wx_v2_homevideo,hsb_wx_v2_homecenter,hsb_wx_v2_homebottom,hsb_wx_v2_homependant'
			})
			if (res._errCode === '0') {
				this.bannerInfo = res._data.bannerList || {}
			}
		},
		async getIndexCardInfo () {
			const [res] = await this.$apis.getIndexCardInfo({
				model: ''
			})
			if (res._errCode === '0') {
				this.modelInfo = res._data || {}
			}
		},
		async getNearStoreInfo () {
			const { latitude, longitude, address = {} } = this.userLocation
			const { cityId } = this.userSelectLocation
			const params = {
				from: '1',
				cityId: cityId || address.cityCode || ''
			}
			if (latitude && longitude) {
				params.latitude = latitude
				params.longitude = longitude
			}
			const [res] = await this.$apis.getNearStoreInfo(params)
			if (res._errCode === '0') {
				const storeData = res._data || {}
				const storePic = storeData.storeType?.storeInfo?.storePic || []
				if (storePic.length) {
					storePic.forEach((imageUrl, i) => {
						storePic[i] = imageUrl.replace(/\/\/s1-1251010403.file.myqcloud.com/, '//s1.huishoubao.com')
					})
				}
				this.storeData = storeData.storeType || {}
			}
		},
		toCitys () {
			uni.navigateTo({
				url: '/pages/citys/index',
				events: {
					updateCityInfo: (cityInfo) => {
						const cityData = cityInfo?.data || {}
						this.$store.commit('setData', {
							key: 'userSelectLocation',
							value: cityData
						})
						// #ifdef WEB
						sessionStorage.setItem('hsb_select_city', JSON.stringify(cityData))
						// #endif
					}
				}
			})
		},
		toSearch () {
			uni.navigateTo({
				url: '/pages/search/index'
			})
		},
		toRecycle ({ evaluateid, productId, classId }) {
			if (evaluateid > 0) {
				return uni.navigateTo({
					url: `/pages/create/index?evaluateId=${evaluateid}&productId=${productId || ''}`
				})
			}
			if (productId) {
				return uni.navigateTo({
					url: `/pages/evaluate/index?productId=${productId || ''}`
				})
			}
			let url = '/pages/products/index'
			if (classId) {
				url += `?cid=${classId}`
			}
			uni.navigateTo({ url })
		}
	},
	async onPullDownRefresh () {
		this.getBannerList()
		this.getIndexCardInfo()
		await this.getNearStoreInfo()
		uni.stopPullDownRefresh()
	},
	onLoad () {
		this.$store.dispatch('getUserLocation', () => {
			this.getNearStoreInfo()
		})
		this.getBannerList()
	},
	onHide () {
		uni.stopPullDownRefresh()
	},
	onShow () {
		this.getIndexCardInfo()
	}
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
